package cn.stylefeng.guns.modular.familynum.controller;

import cn.stylefeng.guns.core.common.annotion.Permission;
import cn.stylefeng.guns.core.common.constant.factory.PageFactory;
import cn.stylefeng.guns.core.common.page.PageInfoBT;
import cn.stylefeng.guns.core.shiro.ShiroKit;
import cn.stylefeng.guns.modular.family.service.IFamilyService;
import cn.stylefeng.guns.modular.system.model.Family;
import cn.stylefeng.guns.modular.system.model.Store;
import cn.stylefeng.guns.modular.system.model.User;
import cn.stylefeng.guns.modular.system.service.IUserService;
import cn.stylefeng.guns.modular.system.warpper.FamilyWarpper;
import cn.stylefeng.guns.modular.system.warpper.FamilynumWarpper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Familynum;
import cn.stylefeng.guns.modular.familynum.service.IFamilynumService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 家庭成员表控制器
 *
 * @author fengshuonan
 * @Date 2021-04-14 09:37:01
 */
@Controller
@RequestMapping("/familynum")
public class FamilynumController extends BaseController {

    private String PREFIX = "/familynum/familynum/";

    private String guanliyuan="1";

    private String huzhu="5";

    private String jiatingchengyuan="6";

    private String yonghu="7";

    @Autowired
    private IFamilynumService familynumService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IFamilyService familyService;

    /**
     * 跳转到家庭成员表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        Integer userid=ShiroKit.getUser().getId();
        Integer familyid=getFamilyid(userid);
        if(familyid!=-1){
            Family family=familyService.selectById(familyid);
            if(family.getCreateid()==userid){
                model.addAttribute("huzhu",1);
            }
            else{
                model.addAttribute("huzhu",0);
            }
        }
        else{
            model.addAttribute("huzhu",0);
        }
        return PREFIX + "familynum.html";
    }

    /**
     * 跳转到添加家庭成员表
     */
    @RequestMapping("/familynum_add")
    public String familynumAdd() {
        return PREFIX + "familynum_add.html";
    }

    /**
     * 跳转到修改家庭成员表
     */
    @RequestMapping("/familynum_update/{familynumId}")
    public String familynumUpdate(@PathVariable Integer familynumId, Model model) {
        Familynum familynum = familynumService.selectById(familynumId);
        model.addAttribute("item",familynum);
        LogObjectHolder.me().set(familynum);
        return PREFIX + "familynum_edit.html";
    }

    /**
     * 获取家庭成员表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    @Permission
    public Object list(String condition) {

//        return familynumService.selectList(null);

        Page<Map<String,Object>> page=new PageFactory<Map<String,Object>>().defaultPage();
        EntityWrapper<Familynum> familynumEntityWrapper=new EntityWrapper<>();
        familynumEntityWrapper.orderBy("id",false);

        Integer userid=ShiroKit.getUser().getId(); //用户id

        EntityWrapper<Familynum> familynumEntityWrapper1=new EntityWrapper<>();
        familynumEntityWrapper1.eq("familyuserid",userid);
        Familynum familynum=familynumService.selectOne(familynumEntityWrapper1);
        System.out.println(familynumService.selectOne(familynumEntityWrapper1));

        //判断是否为空时不能够用 .equals(null)
        if(familynum==null){
            familynumEntityWrapper.eq("familyheadid",-1);
        }
        else{
            familynumEntityWrapper.eq("familyid",familynum.getFamilyid());
        }

        Page<Map<String,Object>>mapPage=familynumService.selectMapsPage(page,familynumEntityWrapper);
        page=new FamilynumWarpper(mapPage).wrap();
        return new PageInfoBT<>(page);
    }

    /**
     * 新增家庭成员  加入家庭
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Familynum familynum) {
        Integer familyid=familynum.getFamilyid();
        Family family=familyService.selectById(familyid);
        familynum.setFamilyname(family.getSpare1());   //家庭名称
//        familynum.setFamilykindid(family.getFamilykindid()); //家庭类型
//        familynum.setFamilyheadid(family.getCreateid());//家庭户主

        Integer userid=ShiroKit.getUser().getId();
        familynum.setFamilyuserid(userid); //家庭成员id
        familynum.setJointime(new Date());


        EntityWrapper<Family> familyEntityWrapper=new EntityWrapper<>();
        familyEntityWrapper.eq("id",familyid);
        Family family1=familyService.selectOne(familyEntityWrapper);

        System.out.println("-----"+family1);
        if(getFamilyid(userid)!=-1){
            Store store=new Store();
            store.setV1("error");
            return store;
        }
        else if(family1==null){
            Store store=new Store();
            store.setV1("error1");
            return store;
        }
        else
            familynumService.insert(familynum);

        //更新用户的家庭id user.version
//        User user=userService.selectById(userid);
//        user.setVersion(familyid);
//
//        //更新用户的角色roleid
//        if(user.getRoleid().equals(yonghu)){
//            user.setRoleid(jiatingchengyuan);
//        }
//        System.out.println("***"+user.getRoleid()+"****");
//        userService.updateById(user);

        return SUCCESS_TIP;
    }

    /**
     * 删除家庭成员表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer familynumId) {

//        Familynum familynum=familynumService.selectById(familynumId);
//        Integer userid=familynum.getFamilyuserid();
//
//        User user=userService.selectById(userid);
//        user.setVersion(null);
//        user.setRoleid(yonghu);
//        userService.updateById(user);

        familynumService.deleteById(familynumId);
        return SUCCESS_TIP;
    }

    /**
     * 退出家庭
     */
    @RequestMapping(value = "/exit")
    @ResponseBody
    public Object exit() {

        Integer userid=ShiroKit.getUser().getId();
//        User user=userService.selectById(userid);
//        user.setVersion(null);
//        user.setRoleid(yonghu);
//        userService.updateById(user);


        EntityWrapper<Familynum> familynumEntityWrapper=new EntityWrapper<>();
        familynumEntityWrapper.eq("familyuserid",userid);

        //获取本条记录id
        Integer id=familynumService.selectOne(familynumEntityWrapper).getId();
        familynumService.deleteById(id);

        return SUCCESS_TIP;
    }

    /**
     * 修改家庭成员表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Familynum familynum) {
        familynumService.updateById(familynum);
        return SUCCESS_TIP;
    }

    /**
     * 家庭成员表详情
     */
    @RequestMapping(value = "/detail/{familynumId}")
    @ResponseBody
    public Object detail(@PathVariable("familynumId") Integer familynumId) {
        return familynumService.selectById(familynumId);
    }

    public Integer getFamilyid(Integer userid){
        EntityWrapper<Familynum> familynumEntityWrapper1=new EntityWrapper<>();
        familynumEntityWrapper1.eq("familyuserid",userid);
        Familynum familynum=familynumService.selectOne(familynumEntityWrapper1);


        //判断是否为空时不能够用 .equals(null)
        if(familynum==null){
            return -1;
        }
        else{
            return familynum.getFamilyid();
        }
    }
}
